package cn.ydw.community.service.impl;

import cn.ydw.community.dao.DiscussPostDao;
import cn.ydw.community.entity.DiscussPost;
import cn.ydw.community.service.DiscussPostService;
import cn.ydw.community.util.SensitiveWordsFilter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dw
 * @since 2020-10-18
 */
@Service
public class DiscussPostServiceImp extends ServiceImpl<DiscussPostDao, DiscussPost> implements DiscussPostService {

    @Autowired
    private DiscussPostDao discussPostDao;

    @Autowired
    private SensitiveWordsFilter sensitiveWordsFilter;


    @Override
    public List<DiscussPost> getDiscussPost(Page<DiscussPost> page){
        return discussPostDao.selectDiscussPosts(page);
    }

    @Override
    public int addDiscussPost(DiscussPost discussPost) {
        if (discussPost == null){
            throw new IllegalArgumentException("参数不能为空!");
        }
        // 转义HTML标记
        discussPost.setTitle(HtmlUtils.htmlEscape(discussPost.getTitle()));
        discussPost.setContent(HtmlUtils.htmlEscape(discussPost.getContent()));
        // 过滤敏感词
        discussPost.setTitle(sensitiveWordsFilter.filter(discussPost.getTitle()));
        discussPost.setContent(sensitiveWordsFilter.filter(discussPost.getContent()));
        return discussPostDao.insert(discussPost);
    }

    /**
     * 查询博客详细信息
     *
     * @param id
     */
    @Override
    public DiscussPost findDiscussPostById(Integer id) {
        DiscussPost post = discussPostDao.selectById(id);
        return post;
    }

    /**
     * 修改博客 评论数
     *
     * @param id
     * @param countComment
     */
    @Override
    public int modifyDiscussCommentCount(Integer id, Integer countComment) {
        return discussPostDao.updateDiscussPostCommentCount(id, countComment);
    }

    @Override
    public int modifyDiscussByStatus(Integer postId, Integer status) {
        return discussPostDao.updateDiscussionByStatus(postId, status);
    }

    @Override
    public int modifyDiscussByType(Integer postId, Integer type) {
        return discussPostDao.updateDiscussionByType(postId, type);
    }

    @Override
    public List<DiscussPost> findDiscussPostByType(Page page, Integer userType) {
        return discussPostDao.selectDiscussPostByType(page, userType);
    }

    @Override
    public List<DiscussPost> findDiscussPostByUserId(Page page,Integer userId) {
        return discussPostDao.selectPageVO(page, userId);
    }

    @Override
    public int updateScore(Integer postId,double score) {
        return discussPostDao.updateScore(postId, score);
    }


}
